<?php

namespace App\Imports;


use App\Models\airline\base\FlightModel;
use App\Models\airline\base\RouteModel;
use App\Models\base\CurrencyModel;
use App\Models\CustomerModel;
use App\Models\OrderModel;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use stdClass;
use App\Models\DeptModel;
use App\Models\AdminRoleModel;

class TaskImport implements ToCollection, WithMultipleSheets
{


    private $result;

    public function __construct() {
        $this->result = new stdClass;
    }

    public function collection(Collection $rows){
        $lines = [];
        $this->result->error = [];


        foreach ($rows as $k => $row) {
            if($k < 1) continue;

            $order_sn = $row[0];
            $nums = $row[1];
            $weight = $row[2];

            if(!$order_sn) continue;

            $find_order = OrderModel::where('sn', $order_sn)->first();
            if(!$find_order) {
                $this->result->error[] = '订单号不存在：' . $order_sn;
                continue;
            }
            $order_id = $find_order->id;

            $lines[] = [
                'order_sn' => $order_sn,
                'order_id' => $order_id,
                'nums' => $nums,
                'weight' => $weight,
            ];
        }

        $this->result->lines = $lines;
    }

    public function sheets(): array {
        return [ 0 => $this];
    }

    public function get_result(){
        return $this->result;
    }


}
